This Bash script takes three command-line arguments and prints each one to the console on a separate line.
npm run import -- "test command arguments"
echo "$1"
echo "$2"
echo "$3"
#!/bin/bash
display_arguments() {
# Check if the correct number of arguments are provided
if [ $# -ne 3 ]; then
echo "Error: Exactly 3 arguments are required."
return 1
fi
# Display the command line arguments
for ((i=1; i<=$#; i++)); do
echo "Argument $i: ${!i}"
done
}
display_arguments "$1" "$2" "$3"
This code snippet simply prints the values of three command-line arguments to the console.
Here's a breakdown:
echo "$1"
: Prints the value of the first command-line argument ($1
).echo "$2"
: Prints the value of the second command-line argument.echo "$3"
: Prints the value of the third command-line argument.
Explanation:
echo
: This is a command in Unix-like operating systems that prints its arguments to the standard output (usually the console)."$1"
, "$2"
, "$3"
: These are variables that represent the command-line arguments passed to the script.
$1
refers to the first argument.$2
refers to the second argument.$3
refers to the third argument."
) around the variables are important because they ensure that any spaces or special characters within the arguments are treated as part of the argument itself.How it works:
When you run this script, you need to provide at least three arguments after the script name. For example:
./script.sh hello world example
The script will then print the following output:
hello
world
example
Let me know if you have any other code snippets you'd like me to explain!